package com.zxc.redis.RateLimiter;

import org.redisson.Redisson;
import org.redisson.api.RRateLimiter;
import org.redisson.api.RateIntervalUnit;
import org.redisson.api.RateType;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

import cn.hutool.core.thread.ThreadUtil;

public class RateLimiterDemo {

	public static void main(String[] args) {
		Config config = new Config();
		config
			//.setCodec(new FstCodec())
			.useSingleServer() //单例模式
			.setAddress("redis://192.168.100.10:6379")//连接地址
			.setDatabase(1) // 数据库索引位置
			.setPassword("fxtc2017") // 密码
			.setClientName("channelbatch") // 客户端名称
			;
		RedissonClient redisson = Redisson.create(config);
		
		RRateLimiter rateLimiter = redisson.getRateLimiter("my-rate-limiter:ctrip1234567");
		rateLimiter.setRate(RateType.OVERALL, 200, 1, RateIntervalUnit.MINUTES);
		
		for (int i = 0; i < 3; i++) {
			
			for (int i1 = 0; i1 < 210; i1++) {
				boolean acquired = rateLimiter.tryAcquire();
				System.out.println("["+i1+"]"+acquired);
			}
			System.out.println("第"+i+"批处理完毕");
			ThreadUtil.safeSleep(30*1000);
		}
		
		
		redisson.shutdown();
	}
}
